#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "Setting.h"
#include "Snake.h"
#include "public.h"

void Snake()
{
    int x = 1, y = 1;
    int i = 0, j = 0;
    int count = 1;
    int start = 1;
    int snake[10][2] = { 0 };
    int left = 0, right = 0, up = 0, down = 0;
    char key;
    int step = 0, choose = 0;

    //第一块的横坐标
    snake[0][0] = 2;
    //第一块的纵坐标
    snake[0][1] = 3;
    //第二块的横坐标
    snake[1][0] = 2;
    //第二块的纵坐标
    snake[1][1] = 2;

    for (i = 0; i <= LINE; ++i)
    {
        for (j = 0; j <= COLUMN; ++j)
        {
            square[i][j] = 0;
            square[j][0] = -1;
            square[0][j] = -1;
            square[j][COLUMN] = -1;
            square[LINE][j] = -1;
        }
    }

    system("cls");
    for (i = 0; i <= LINE; ++i)
    {
        for (j = 0; j <= COLUMN; ++j)
        {
            if (square[i][j] == -1)
            {
                printf("|||");
            }
            else
            {
                printf("%3d", 0);
            }
        }
        printf("\n");
    }
    gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);    //黄色
    printf("%3d", 1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色
    gotoxy(snake[0][0] * 3 + 1, snake[0][1] + 1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
    printf("%3d", 1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色

    while (start == 1)
    {
        if (snake[0][0] < COLUMN - 1 &&
            snake[0][1] < LINE - 1 &&
            snake[0][0] >1 &&
            snake[0][1] >1)
        {
            if (_kbhit())
            {
                key = _getch();
                if (key == 'p' || key == 'P')
                {
                    pause();
                }
                //0:right,1:down,2:left,3:up
                switch (key)
                {
                case 'd':
                case 'D': choose = 0; break;
                case 's':
                case 'S': choose = 1; break;
                case 'a':
                case 'A': choose = 2; break;
                case 'w':
                case 'W': choose = 3; break;
                default: break;
                }
            }

            switch (choose)
            {
            case 0:
                //第二块清除
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                printf("%3d", 0);

                // 第二块继承第一块
                snake[1][0] = snake[0][0];
                snake[1][1] = snake[0][1];

                //第二块显示黄色,4显示红色,6显示黄色
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色

                //第一块横坐标+1
                snake[0][0] += 1;
                snake[0][1] += 0;

                //第一块显示红色,4显示红色,6显示黄色
                gotoxy(snake[0][0] * 3 + 1, snake[0][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色
                break;
            case 1:
                //第二块清除
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                printf("%3d", 0);

                // 第二块继承第一块
                snake[1][0] = snake[0][0];
                snake[1][1] = snake[0][1];

                //第二块显示黄色,4显示红色,6显示黄色
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色

                //第一块纵坐标+1
                snake[0][0] += 0;
                snake[0][1] += 1;

                //第一块显示红色
                //4显示红色
                //6显示黄色
                gotoxy(snake[0][0] * 3 + 1, snake[0][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色
                break;

            case 2:
                //第二块清除
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                printf("%3d", 0);

                // 第二块继承第一块
                snake[1][0] = snake[0][0];
                snake[1][1] = snake[0][1];

                //第二块显示黄色,4显示红色,6显示黄色
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色

                //第一块横坐标-1
                snake[0][0] -= 1;
                snake[0][1] += 0;

                //第一块显示红色
                //4显示红色
                //6显示黄色
                gotoxy(snake[0][0] * 3 + 1, snake[0][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色
                break;

            case 3:
                //第二块清除
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                printf("%3d", 0);

                // 第二块继承第一块
                snake[1][0] = snake[0][0];
                snake[1][1] = snake[0][1];

                //第二块显示黄色,4显示红色,6显示黄色
                gotoxy(snake[1][0] * 3 + 1, snake[1][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色

                //第一块纵坐标-1
                snake[0][0] += 0;
                snake[0][1] -= 1;

                //第一块显示红色
                //4显示红色
                //6显示黄色
                gotoxy(snake[0][0] * 3 + 1, snake[0][1] + 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
                printf("%3d", 1);
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);    //字体显示黑色
                break;
            default:break;
            }

            step++;

            gotoxy(1, LINE + 2);
            printf("Step:%d\n", step);
            printf("Location:%3d,%3d\n", snake[0][1], snake[0][0]);

            Sleep(100);
        }
        else
        {
            //离开循环标志
            start = 0;
            //打印结束语句
            gotoxy(1, LINE + 5);
            printf("The total steps are:%d\n", step);
            printf("Game over!\n");
        }
    }
    system("pause");
}
